<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- 引入 Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <title>身份管理</title>
  <style>
    body {
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      background-color: #f8f9fa;
      padding: 20px;
    }

    .container {
      max-width: 600px;
      margin: auto;
    }

    .nav-link.active {
      background-color: #007bff;
      color: white !important;
      font-weight: bold;
    }

    .form-label {
      font-weight: bold;
    }

    .form-control {
      margin-bottom: 1rem;
    }

    .btn-primary {
      background-color: #007bff;
      border-color: #007bff;
    }

    .btn-primary:hover {
      background-color: #0069d9;
      border-color: #0062cc;
    }

    .btn-danger {
      background-color: #dc3545;
      border-color: #dc3545;
    }

    .btn-danger:hover {
      background-color: #c82333;
      border-color: #bd2130;
    }
  </style>
</head>
<body>
<nav class="mb-4">
  <ul class="nav nav-tabs">
    <li class="nav-item">
      <a class="nav-link" th:href="@{/courier/order_management}">订单管理</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" th:href="@{/courier/earnings_management}">收益管理</a>
    </li>
    <li class="nav-item">
      <a class="nav-link active" th:href="@{/courier/identity_management}">身份管理</a>
    </li>
  </ul>
</nav>

<div class="container">
  <div class="card mb-4">
    <div class="card-header bg-primary text-white">
      <h1 class="card-title mb-0">身份管理</h1>
    </div>
    <div class="card-body">
      <form class="needs-validation" novalidate>
        <div class="mb-3">
          <label for="name" class="form-label">姓名：</label>
          <input type="text" class="form-control" id="name" name="name" required>
          <div class="invalid-feedback">
            请输入您的姓名
          </div>
        </div>
        <div class="mb-3">
          <label for="id-number" class="form-label">身份证号码：</label>
          <input type="text" class="form-control" id="id-number" name="id-number" required>
          <div class="invalid-feedback">
            请输入有效的身份证号码
          </div>
        </div>
        <div class="mb-3">
          <label for="phone" class="form-label">联系方式：</label>
          <input type="text" class="form-control" id="phone" name="phone" required>
          <div class="invalid-feedback">
            请输入有效的联系方式
          </div>
        </div>
        <div class="d-grid gap-2 d-md-flex justify-content-md-end">
          <button type="button" class="btn btn-primary" onclick="registerCourier()">注册配送员身份</button>
          <button type="button" class="btn btn-danger" onclick="cancelRegistration()">注销配送员身份</button>
        </div>
      </form>
    </div>
  </div>
</div>

<script>
  (function () {
    'use strict';

    var forms = document.querySelectorAll('.needs-validation');

    Array.prototype.slice.call(forms)
      .forEach(function (form) {
        form.addEventListener('submit', function (event) {
          if (!form.checkValidity()) {
            event.preventDefault();
            event.stopPropagation();
          }

          form.classList.add('was-validated');
        }, false);
      });
  })();

  const courierId = 1;

  function registerCourier() {
    const name = document.getElementById('name').value;
    const idNumber = document.getElementById('id-number').value;
    const phone = document.getElementById('phone').value;

    fetch(`/courier/${courierId}/register`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ name, idNumber, phone })
    })
      .then(response => response.json())
      .then(data => {
        if (data.success) {
          alert('注册成功');
        } else {
          alert('注册失败: ' + data.message);
        }
      })
      .catch(error => {
        console.error('注册请求失败:', error);
        alert('注册请求失败，请稍后重试');
      });
  }

  function cancelRegistration() {
    fetch(`/courier/${courierId}/cancel-registration`, {
      method: 'POST'
    })
      .then(response => response.json())
      .then(data => {
        if (data.success) {
          alert('注销成功');
        } else {
          alert('注销失败: ' + data.message);
        }
      })
      .catch(error => {
        console.error('注销请求失败:', error);
        alert('注销请求失败，请稍后重试');
      });
  }
</script>

<!-- 引入 Bootstrap JS 和依赖 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>